<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';

$username = $_SESSION['username'];

$sql = "SELECT firstname, lastname, type FROM users WHERE username = '$username'";

  if ($db->query($sql)) {  
    } else {
      echo "Error: " . $sql . "<br>" . $db->error;
    }

  $result = $db->query($sql);
  $row = $result->fetch_assoc();
  $type = $row['type'];

?>

<style type="text/css">
	.panel{
		font-size:15px;
	}

	.list-group-item{
		border:none;
	}



	#playerinfo{
			font-size:14px;
	}

	.editprofileentry, .deleteprofileentry, .editgeneralprofile {
		opacity:0.8;
	}

	.editprofileentry:hover, .deleteprofileentry:hover, .editgeneralprofile:hover {
		opacity:1.0;
	}

	#editentryform{
		position:absolute;
		left:0px;
		top:10px;
	}

	#deleteentryform{
		position:absolute;
		left:35px;
		top:10px;
	}

	#editgeneralprofileform{
		//position:absolute;
		//top:20px;
		//right:150px;
		float:right;
	}




</style>

<?php

$datenow = date("Y/m/d");
$yearnow = date("Y");
$monthnow = (int)date("m");
$yearprev = $yearnow - 1;
$yearnext = $yearnow + 1;

if ($monthnow < 8) {

	$currentseason = "$yearprev/$yearnow";

} else if ($monthnow >= 8) {

	$currentseason = "$yearnow/$yearnext";
}

?>

<h2>Team Actionpoints <?= $currentseason; ?> </h2>

<br>

<div class="container-fluid">
<div class="row">

<div class="col-md-2"></div>

<div class="col-md-8" id="maincontent">

<div><button type="submit" class="btn btn-success" id="addnewnote"> Add new </button>  

<button type="submit" class="btn btn-default pull-right" id="archive" class="archive"> Archive </button></div>

<br><br>

<div class="table-responsive">
	      		<h3>General</h3>
			<table class="table table-hover table-striped">
			    <thead>
			      <tr>
			        <th>Visibility</th>
			        <th>Text</th>
			        <th>Edit</th>
				 </tr>
			    </thead>
			    <tbody>

<?php

include_once __DIR__ . '/db_bb_connect.php';


if ($monthnow < 8) {

	$sql2 = "SELECT * FROM attentionpointsteams
	WHERE archived = 'no' 
	AND date_create
	BETWEEN '$yearprev/08/01' AND '$yearnow/08/01'
	ORDER BY date DESC";

} else if ($monthnow >= 8) {

$sql2 = "SELECT * FROM attentionpointsteams
	WHERE archived = 'no' 
	AND date_create
	BETWEEN '$yearnow/08/01' AND '$yearnext/08/01'
	ORDER BY date DESC";
}

//$sql2 = "SELECT * FROM attentionpointsteams
//ORDER BY date DESC";

if ($db->query($sql2)) { 

$result2 = $db->query($sql2);

while ( $row2 = $result2->fetch_assoc() ) {

	$ID = $row2['ID']; 
  	$category = $row2['category']; 
  	$text = nl2br($row2['text']);
  	$visibility = $row2['visibility'];
	
	if ($category == 'general'){	

		echo "<tr id='$ID'>";
		echo "<td>  
				<form method=\"post\" action=\"\" class=\"hidden-print\"> 
  				<input type=\"hidden\" name=\"ID\" value=\"$ID\">
				<select class=\"form-control\" name=\"decision\">";
				if ($visibility == 'yes') {
					echo "<option value=\"$visibility\" selected=\"selected\">$visibility</option>";
					echo "<option value=\"no\">no</option>";
					
				} else if ($visibility == 'no') {
					echo "<option value=\"$visibility\" selected=\"selected\">$visibility</option>";
					echo "<option value=\"yes\" >yes</option>";
				}

		echo "		</select>
				</form> <div id=\"message$ID\"></div>
		</td>";
		echo "<td> $text </td>";
		echo "<td> 
			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<input type='image' class='editteamattentionpoint pull-left'  src='./images/edit_button.png' width=20px height=20px/> 
			</form>

			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<input type='image' class='deleteteamattentionpoint pull-left' src='./images/delete_button.png' width=20px height=20px/>
			</form>
			<br><br>
			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<button type='submit' name='submit' class='btn btn-sm btn-warning archive'>archive</button>
			</form>
			</td>";
 		echo "</tr>";
}} ?>
			</tbody>
		</table>
	      
</div>

<br>
<div class="table-responsive">
	      		<h3>Game</h3>
			<table class="table table-hover table-striped">
			    <thead>
			      <tr>
			        <th>Visibility</th>
			        <th>Text</th>
			        <th>Edit</th>
				 </tr>
			    </thead>
			    <tbody>

<?php

$result2 = $db->query($sql2);

while ( $row2 = $result2->fetch_assoc() ) {

	$ID = $row2['ID']; 
  	$category = $row2['category']; 
  	$text = nl2br($row2['text']);
  	$visibility = $row2['visibility'];
	
	if ($category == 'game'){	

		echo "<tr id='$ID'>";
		echo "<td>  
				<form method=\"post\" action=\"\" class=\"hidden-print\"> 
  				<input type=\"hidden\" name=\"ID\" value=\"$ID\">
				<select class=\"form-control\" name=\"decision\">";
				if ($visibility == 'yes') {
					echo "<option value=\"$visibility\" selected=\"selected\">$visibility</option>";
					echo "<option value=\"no\">no</option>";
					
				} else if ($visibility == 'no') {
					echo "<option value=\"$visibility\" selected=\"selected\">$visibility</option>";
					echo "<option value=\"yes\" >yes</option>";
				}

		echo "		</select>
				</form> <div id=\"message$ID\"></div>
		</td>";
		echo "<td> $text </td>";
		echo "<td> 
			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<input type='image' class='editteamattentionpoint pull-left'  src='./images/edit_button.png' width=20px height=20px/> 
			</form>

			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<input type='image' class='deleteteamattentionpoint pull-left' src='./images/delete_button.png' width=20px height=20px/>
			</form>
						<br><br>
			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<button type='submit' name='submit' class='btn btn-sm btn-warning archive'>archive</button>
			</form>
			</td>";
 		echo "</tr>";
}} ?>
			</tbody>
		</table>
</div>


<br>
<div class="table-responsive">
				<h3>Training</h3>
			<table class="table table-hover table-striped">
			    <thead>
			      <tr>
			        <th>Visibility</th>
			        <th>Text</th>
			        <th>Edit</th>
				 </tr>
			    </thead>
			    <tbody>

<?php

$result2 = $db->query($sql2);

while ( $row2 = $result2->fetch_assoc() ) {

	$ID = $row2['ID']; 
  	$category = $row2['category']; 
  	$text = nl2br($row2['text']);
  	$visibility = $row2['visibility'];
	
	if ($category == 'training'){	

		echo "<tr id='$ID'>";
		echo "<td>  
				<form method=\"post\" action=\"\" class=\"hidden-print\"> 
  				<input type=\"hidden\" name=\"ID\" value=\"$ID\">
				<select class=\"form-control\" name=\"decision\">";
				if ($visibility == 'yes') {
					echo "<option value=\"$visibility\" selected=\"selected\">$visibility</option>";
					echo "<option value=\"no\">no</option>";
					
				} else if ($visibility == 'no') {
					echo "<option value=\"$visibility\" selected=\"selected\">$visibility</option>";
					echo "<option value=\"yes\" >yes</option>";
				}

		echo "		</select>
				</form> <div id=\"message$ID\"></div>
		</td>";
		echo "<td> $text </td>";
		echo "<td> 
			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<input type='image' class='editteamattentionpoint pull-left'  src='./images/edit_button.png' width=20px height=20px/> 
			</form>

			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<input type='image' class='deleteteamattentionpoint pull-left' src='./images/delete_button.png' width=20px height=20px/>
			</form>
						<br><br>
			<form> 
			<input type='hidden' name='ID' value='$ID'/>
			<button type='submit' name='submit' class='btn btn-sm btn-warning archive'>archive</button>
			</form>
			</td>";
 		echo "</tr>";
}} ?>
			</tbody>
		</table>


<?php   	
		} else {
      echo "Error: " . $sql2 . "<br>" . $db->error;
    }	  	
?>



</div>

<div class="col-md-2"></div>

</div>
</div>

<script type="text/javascript">
	
$("#addnewnote").on("click", function(e) {
	$.ajax({
  type: "GET",
  url: "./actionpoints_team_add.php",
  success: function(data) {
    $("#maincontent").html(data);
  },
  error: function (xhr, status, error) {
    $("#maincontent").html(xhr.responseText); 
  }
  }); 
  e.preventDefault();
});


$(".deleteteamattentionpoint").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./includes/handlers/coachtips_delete_handler.php",
  			data: postData,
  			success: function(data) { 
 				$("#maincontent").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});


$(".editteamattentionpoint").on("click", function(e) {
		postData = $(this.form).serialize();
		$.ajax({
 			type: "POST",
  			url: "./actionpoints_team_edit.php",
  			data: postData,
  			success: function(data) { 
 				$("#maincontent").html(data);				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});

$(".archive").on("click", function(e) {
		postData = $(this.form).serialize();
		var ID = $(this.form).find('input[name=ID]').val();
		$.ajax({
 			type: "POST",
  			url: "./includes/handlers/actionpoints_team_archive_item_handler.php",
  			data: postData,
  			success: function(data) {
 				$("#" + ID).html("<td colspan='4'>" + data + "</td>");				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});


$("#archive").on("click", function(e) {
	$.ajax({
  type: "GET",
  url: "./actionpoints_team_archive.php",
  success: function(data) {
    $("#maincontent").html(data);
  },
  error: function (xhr, status, error) {
    $("#maincontent").html(xhr.responseText); 
  }
  }); 
  e.preventDefault();
});


$("select").on( "change", function(e) {
  var ID = $(this.form).find('input[name=ID]').val();
  postData = $(this.form).serialize();
  $.ajax({
    type: "POST",
    url: "./includes/handlers/coachtips_visibility_handler.php",
    data: postData,
    success: function(data) { 
        $("#message" + ID).html(data);       
        },
    error: function (xhr, status, error) {
         alert(xhr.responseText);
  		}
  });
  e.preventDefault();
 });


	

</script>


